﻿#if NET45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;

namespace JESAI.HtmlTemplate.Pdf.MemoryCache
{
    public class HttpRuntimeCacheManager : ICacheManager
    {
        public HttpRuntimeCacheManager()
        {
        }

        protected System.Web.Caching.Cache Cache
        {
            get
            {
                return HttpRuntime.Cache;
            }
        }
        public bool Exists(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            if (Cache[key] == null)
            {
                return false;
            }
            return true;
        }
        public object Get(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            if (Cache[key] != null)
            {
                return Cache[key];
            }
            return null;
        }

        public T Get<T>(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            return (T)Get(key);
        }

        public void Set(string key, object data)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            Set(key, data, 60);
        }

        public void Set(string key, object data, int cacheTime)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            Set(key, data, cacheTime, CacheItemPriority.Default);
        }

        public void Set(string key, object data, CacheDependency dependency)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            if (data == null) return;
            Cache.Insert(key, data, dependency);
        }

        public void Set(string key, object data, int cacheTime, CacheItemPriority priority)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            Set(key, data, null, cacheTime, priority);
        }

        private void Set(string key, object data, CacheDependency dependency, int cacheTime, CacheItemPriority priority)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            if (data == null) return;
            var absoluteExpiration = DateTime.Now.AddMinutes(cacheTime);
            Cache.Insert(key, data, dependency, absoluteExpiration, TimeSpan.Zero, CacheItemPriority.Default, null);
        }

        public void NoAbsoluteExpirationSet(string key, object data,int NoAbsoluteTime)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            Cache.Insert(key, data, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(NoAbsoluteTime));
        }

        public void Remove(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));       
                Cache.Remove(key);
        }

        public void RemoveMulti(string key)
        {
            var cacheEnum = Cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                if (cacheEnum.Key.ToString().Contains(key))
                {
                    Cache.Remove(cacheEnum.Key.ToString());
                }
            }
        }

        public void Clear()
        {
            var cacheEnum = Cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                Cache.Remove(cacheEnum.Key.ToString());
            }
        }

        public void Set(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            Cache.Insert(key, value, null, DateTime.Now.AddMilliseconds(expiressAbsoulte.TotalMilliseconds), expiresSliding);
        }

        public void Set(string key, object value, TimeSpan expiresIn, bool isSliding = false)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException(nameof(key));
            if (value == null) return;
            var absoluteExpiration = DateTime.Now.AddMinutes(expiresIn.TotalMilliseconds);
            if(isSliding)
                Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, expiresIn);
            else
                Cache.Insert(key, value, null, absoluteExpiration, TimeSpan.Zero);
        }

        public void Dispose()
        {
            
        }
    }
}
#endif